package edu.qd.learn.newchapter.chapter_thread.day02;

/**
 * @author 布衣
 */
public class Third extends Thread {

    static int ticket = 1;
    static Object obj = new Object();//不管创建多少个对象，obj都是共享的

    @Override
    public void run() {

        //同步代码块

        while (true) {
//            System.out.println();

//            System.out.flush();
            synchronized (obj) {//锁对象一定要是唯一的
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                if (ticket < 101) {
                    System.out.println(getName() + "正在出售第" + ticket++ + "张票");
                } else {
                    break;
                }
            }
        }
    }

    public Third() {

    }

    public Third(String name) {
        super(name);
    }

    public static void main(String[] args) {
        Third t1 = new Third("窗口1");
        Third t2 = new Third("窗口2");
        Third t3 = new Third("窗口3");

        t1.start();
        t2.start();
        t3.start();
    }

}